package com.jack.hhitseat.utils;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;

import com.baidu.aip.ocr.AipOcr;

public class AipOcrUtil {
	//设置APPID/AK/SK
    public static final String APP_ID = "17356241";
    public static final String API_KEY = "mouqc2w7664oiIgfeZPqjc44";
    public static final String SECRET_KEY = "nIN6SyK4LHkvZ0WzbPxKgHKhkeLPOziP";
    public static final String URL = "http://seat.hhit.edu.cn/ClientWeb/pro/page/image.aspx";
    public static AipOcr client = null;

    public static AipOcr getAipOcr() {
    	if(client == null) {
    		client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
	        client.setConnectionTimeoutInMillis(30000);
	        client.setSocketTimeoutInMillis(120000);
    	}
    	return client;
    }
    
    public static String getCode(byte[] file) {
    	if(client == null) {
    		getAipOcr();
    	}
    	 // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        // 通用文字识别, 图片参数为远程url图片
       //JSONObject res = client.webImageUrl(URL, options, session);
        try {
        	JSONObject res = client.basicAccurateGeneral(file, options);
        	return res.toString().split("\"words\":\"")[1].split("\"}],")[0];
        }catch (Exception e) {
        	e.printStackTrace();
			return "0";
		}
    }
    
    public static String repalce(String code) {
    	if(code.contains("加")) {
    		code =code.replace("加", "+"); 
		}
    	if(code.contains("减")) {
    		code =code.replace("减", "-"); 
		}
    	if(code.contains("乘")) {
    		code =code.replace("乘", "*"); 
		}
    	if(code.contains("除")) {
    		code =code.replace("除", "/"); 
		}
    	if(code.contains("add")) {
    		code =code.replace("add", "+"); 
		}
        if(code.contains("jia")) {
        	code =code.replace("jia", "+"); 
		}
        if(code.contains("ja")) {
        	code =code.replace("ja", "+"); 
		}
        if(code.contains("ia")) {
        	code =code.replace("ia", "+"); 
		}
        if(code.contains("ji")) {
        	code =code.replace("jia", "+"); 
		}
        if(code.contains("jian")) {
        	code =code.replace("jian", "-"); 
		}
        if(code.contains("cheng")) {
        	code =code.replace("cheng", "*"); 
		}
        return code;
    }
    
    public static String getResult(String code) {
    	code = code.replace(" ", ""); 
    	try {
	    	String replace = repalce(code);
	    	if(isContainChinese(code)) {
	    		if(!code.contains("=") && !code.equals(replace)) {
	    	       	return CalcUtl.calculate(replace)+"";
	    	    }else if(code.contains("=") && !code.equals(replace)){
	    	        return CalcUtl.calculateByEques(replace)+"";
	    	    }
	 			return code;
	    	}
	
	        if(!code.contains("=")  && !code.equals(replace)) {
	        	return CalcUtl.calculate(replace)+"";
	        }else if(code.contains("=") && !code.equals(replace)){
		        return CalcUtl.calculateByEques(replace)+"";
		    }
	        
	        if(code.contains("?") || code.contains("=")) {
	        	return CalcUtl.calculateByEques(replace)+"";
	        }
	        if(code.contains("+")  || code.contains("*")) {
	        	return CalcUtl.calculate(replace)+"";
	        }
	    	
	    	for(int i=0; i<code.length(); i++) {
	    		if(isWord(code.charAt(i))) {
	    			return code;
	    		}
	    	}
    	}catch (Exception e) {
    		return code;
		}
    	return code;
    }
    
   
    /**
     * 判断字符串里面是不是包含中文
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
    /**
     * 判断一个字符不是0-9
     */
    public static boolean isWord(char c) {
        String regEx = "[\\D]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher("" + c);
        return m.matches();
    }
    
    public static void main(String[] args) {
//    	String codes[] = {"NW 8 WLJ ","?jia55=99",
//    			"?add55=99","55?add=99","2+5add8cheng3jia4",
//    			"和尚","1乘6+6+5","?+71=93","78加？=82"};
//    	for(String code: codes) {
//    		long s = System.currentTimeMillis();
//    		System.out.println("code======"+code);
//        	System.out.println("计算结果====" + getResult(code));
//        	System.out.println("time======" + (System.currentTimeMillis() - s));
//        	System.out.println("=======================================================");
//    	}
    	
    	long s = System.currentTimeMillis();
//    	System.out.println("计算结果====" + getResult(getCode()));
    	System.out.println("time======" + (System.currentTimeMillis() - s));
    }
}
